package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.ArticleDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.vo.PageInfo;

@Repository("articleDao")
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao {

	// 分页查询所有文章
	public List<Article> findArticle(PageInfo pageInfo, Integer status) {
		String hql = "from Article where status = :status order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		String hql2 = "select count(*) from Article where status = :status";

		// 获得表中总的条目数目
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status).uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	// 分页查询个人所有文章
	public List<Article> findPersonArticle(Integer id, PageInfo pageInfo,
			Integer status) {
		String hql = "from Article where status = :status and userdetail_id = :id order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setInteger("id", id)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Article where status = :status and userdetail_id = :id";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status).setInteger("id", id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Article> findArticleByRead(PageInfo pageInfo, Integer status) {
		String hql = "from Article where status = :status order by reading desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		String hql2 = "select count(*) from Article where status = :status";

		// 获得表中总的条目数目
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status).uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Article> getArticleByCollect(PageInfo pageInfo, Integer status) {
		String hql = "from Article where status = :status order by collecting desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		String hql2 = "select count(*) from Article where status = :status";

		// 获得表中总的条目数目
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status).uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Article> getArticleByReward(PageInfo pageInfo, Integer status) {
		String hql = "from Article where status = :status order by reward desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		String hql2 = "select count(*) from Article where status = :status";

		// 获得表中总的条目数目
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status).uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Article> searchArticle(PageInfo pageInfo, String search) {
		String hql = "from Article where status = 1 and title like :search order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Article> list = getCurrentSession()
				.createQuery(hql)
				.setString("search", "%"+search+"%")
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		String hql2 = "select count(*) from Article where status = 1 and title like :search";

		// 获得表中总的条目数目
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setString("search", "%"+search+"%").uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

}
